package com.tim.app.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tim.app.R;


public class UpdateDialog extends Dialog implements View.OnClickListener {

    private DialogButtonOnClickListener listener_1, listener_2;
    private Context context;
    private TextView tvUpdateInfo;
    private Button btnConfrim;
    private View llCloseContainer;
    private ImageView ivClose;
    private TextView tvVersion;

    public UpdateDialog(Context context) {
           super(context, R.style.CommonDialog);
        setCanceledOnTouchOutside(true);
        getWindow().getAttributes().width = -1;
        getWindow().getAttributes().height = -2;
        getWindow().getAttributes().y = 0;
        getWindow().setGravity(Gravity.CENTER_VERTICAL);
        getWindow().setAttributes(getWindow().getAttributes());
        if (context instanceof Activity)
            setOwnerActivity((Activity) context);
        this.context = context;

        setContentView(R.layout.update_dialog_tip);
        tvUpdateInfo = (TextView) findViewById(R.id.tvUpdateInfo);
        btnConfrim = (Button) findViewById(R.id.btnConfrim);
        llCloseContainer = findViewById(R.id.llCloseContainer);
        ivClose = (ImageView) findViewById(R.id.ivClose);
        tvVersion = (TextView) findViewById(R.id.tvVersion);
    }

    public void setTextDes(String version, String updateInfo) {
        tvVersion.setText(version);
        tvUpdateInfo.setText(updateInfo);
    }


    public void setButtonListener(DialogButtonOnClickListener clickListener) {
        this.listener_1 = clickListener;
        this.btnConfrim.setOnClickListener(this);
        setCanceledOnTouchOutside(false);
    }

    public void setButtonCloseListener(DialogButtonOnClickListener clickListener) {
        this.listener_2 = clickListener;
        this.ivClose.setOnClickListener(this);
        setCanceledOnTouchOutside(false);
    }


    public interface DialogButtonOnClickListener {
        void onClick(View button, UpdateDialog dialog);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnConfrim:
                if (listener_1 != null) {
                    listener_1.onClick(v, this);
                }
                break;
            case R.id.ivClose:
                if (listener_2 != null) {
                    listener_2.onClick(v, this);
                }
                break;
            default:
                break;
        }
    }
}
